/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);
//表格初始化配置
var tableInit = function () {
    var columns = [
        {
            title: '全选',
            field: 'select',
            //复选框
            checkbox: true,
            width: 25,
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'id',
            title: 'ID',
            align: 'center'
        }, {
            field: 'service_version_type_name',
            title: '服务套餐类别',
            align: 'center'
        },
        {
            field: 'img_url',
            title: 'LOGO',
            align: 'center',
            formatter: function (value) {
                if (!empty(value)) {
                    var img_html = "<a href='" + value + "' title='点击查看大图' target='_blank'>" +
                        "<img src='" + value + "' width='40' onerror='imgNotfind(event);' /></a>";
                    return img_html;
                } else {
                    return "<img src='" + value + "' width='40' onerror='imgNotfind(event);' />";
                }
            }
        },
        {
            field: 'businessLicenseImgUrl',
            title: '营业执照',
            align: 'center',
            formatter: function (value) {
                if (!empty(value)) {
                    var img_html = "<a href='" + value + "' title='点击查看大图' target='_blank'>" +
                        "<img src='" + value + "' width='40' onerror='imgNotfind(event);' /></a>";
                    return img_html;
                } else {
                    return "<img src='" + value + "' width='40' onerror='imgNotfind(event);' />";
                }
            }
        },
        {
            field: 'holdBusinessLicenseImgUrl',
            title: '手持营业执照',
            align: 'center',
            formatter: function (value) {
                if (!empty(value)) {
                    var img_html = "<a href='" + value + "' title='点击查看大图' target='_blank'>" +
                        "<img src='" + value + "' width='40' onerror='imgNotfind(event);' /></a>";
                    return img_html;
                } else {
                    return "<img src='" + value + "' width='40' onerror='imgNotfind(event);' />";
                }
            }
        },
        {
            field: 'applyName',
            title: '申请人',
            align: 'center'
        }, {
            field: 'applyWxTel',
            title: '申请人微信关联电话',
            align: 'center'
        }, {
            field: 'applyTel',
            title: '申请人常用电话',
            align: 'center'
        },
        {
            field: 'name',
            title: '名称',
            align: 'center'
        },{
            field: 'short_name',
            title: '简称',
            align: 'center',
            visible: false,
        },{
            field: 'location',
            title: '地址',
            align: 'center'
        },{
            field: 'tel',
            title: '电话',
            align: 'center',
            visible: false,
        },
        {
            field: 'email',
            title: '邮箱',
            align: 'center',
            visible: false,
        }, {
            field: 'qq',
            title: 'QQ',
            align: 'center',
            visible: false,
        }, {
            field: 'reg_person',
            title: '注册人',
            align: 'center',
            visible: false,
        },{
            field: 'reg_time',
            title: '注册时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        },{
            field: 'reg_money',
            title: '注册资金',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                if(!empty(value))
                    return value +" 人民币";
                return "-"
            }
        },
        {
            field: 'reg_type',
            title: '注册类型',
            align: 'center',
            visible: false,
        },  {
            field: 'run_status',
            title: '运营状态',
            align: 'center',
        }, {
            field: 'run_industry',
            title: '经营行业',
            align: 'center'
        },{
            field: 'credit',
            title: '信用代码',
            align: 'center',
            visible: false,
        },{
            field: 'webSite',
            title: '官网',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                if(!empty(value)){
                    if("http" == value.substring(0,4)){
                        return "<a href='"+value+"' target='_blank'>"+value +"</a>"
                    }else {
                        return "<a href='http://"+value+"' target='_blank'>"+value +"</a>"
                    }
                }

                return "-"
            }
        },{
            field: 'description',
            title: '描述',
            align: 'center',
            visible: false,
        },
        {
            field: 'remarks',
            title: '审核备注',
            align: 'center',
            visible: false,
        },
        {
            field: 'gmt_create',
            title: '创建时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        },
        {
            field: 'gmt_modified',
            title: '更新时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        }
    ];

    //请求服务数据时所传参数
    function queryParams(params) {
        return {
            pageNo: params.pageNumber,
            pageSize: params.pageSize,
            companyId:companyId,
            schoolId:schoolId,
            isReal:$("#isReal").val(),
            searchText: params.searchText,
        }
    }

    var tableConfig = $.extend(bootstrapTableConfig(_HOST + _API.business_companyApply_list, queryParams, columns), {
        //这里是其他的一些个性化的 bootstrap-table options
        //例如 覆盖一些公共的配置
        //例如 添加一些私有的配置
        showColumns: true,//列显示操作按钮
        ajaxOptions: { //设置自定义请求头
            headers: {
                'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
                'permCoding': 'companyApply:list'
            }
        },
        //例如格式化数据
        responseHandler: function (res) {
            //在ajax获取到数据，渲染表格之前，修改数据源
            //console.log(res)
            if (res.success) {
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token, res.token);
            }
            return res;
        }
    });
    //调用实例化
    $('#bootstrapTableEvents').bootstrapTable(tableConfig);
    $(".search input").attr('placeholder', '输入名称/类型搜索');
};

var app = new Vue({
    el: '#app',
    data: {
        permissions: ['companyApply:add', 'companyApply:update', 'companyApply:delete', 'companyApply:list', 'companyApply:deal'],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        selectStatus: function () {
            $('#bootstrapTableEvents').bootstrapTable('refresh');
        },
        update:function (index) {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length != 1) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }
            console.log(list[0])
            var param = list[0]
            var confirm = layer.confirm('<textarea id="texta" style="width: 100%;height: 100%"></textarea>', {
                title:"请输入拒绝审核理由",
                btn: ['确定', '取消'], //按钮
                shadeClose: false,
                area:['50%','50%']
            }, function () {
                console.log($("#texta").val());
                param.isReal = 0;
                param.remarks = $("#texta").val();
                console.log(param)
                Post(that,_HOST+_API.business_companyApply_update,param,true,that.permissions[index])
                 .then(function (response) {
                     // 响应成功回调
                     console.log(response)
                     if(response){
                         if(response.success){
                             layer.msg("操作成功！", {time: 1500, icon:1});
                             setTimeout(function () {
                                $('#bootstrapTableEvents').bootstrapTable('refresh');
                                layer.closeAll();
                             }, 1500)
                         }else {
                            layer.msg("操作失败！", {time: 1500, icon:5});
                         }
                     }
                 })
            });
        },
        deal: function(index) {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length != 1) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }
            if (empty(list[0].applyName)) {
                layer.msg('当前申请缺少申请人信息！', {time: 1500, icon: 5});
                return;
            }
            console.log(list[0])

            var index = layer.open({
                type: 2,
                title: "审核通过",
                content: "edit.html?id="+list[0].id,
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
            });
            layer.full(index);

        },
        postDeal: function(ids,isReal,index) {
            var that = this;
            Post(that, _HOST + _API.business_companyApply_deal+"?ids=" + ids+"&isReal="+isReal, {}, true, that.permissions[index])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    layer.closeAll();
                    $('#bootstrapTableEvents').bootstrapTable('refresh');
                    if (response) {
                        if (response.success) {
                            layer.msg(response.msg, {time: 1500, icon: 1});
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                })
        },
        //删除
        del: function (index) {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0) {
                layer.msg('请至少选中一条数据', {time: 1500, icon: 5});
                return;
            }

            var ids = "";
            for (let i = 0; i < list.length; i++) {
                ids += list[i].id;
                if ((i+1)!=list.length) {
                    ids += ",";
                }
            }

            var confirm = layer.confirm('确定删除选中项？', {
                btn: ['确定', '取消'], //按钮
                shadeClose: false,
            }, function () {
                Post(that, _HOST + _API.business_companyApply_delete+"?ids=" + ids, {}, true, that.permissions[index])
                    .then(function (response) {
                        // 响应成功回调
                        console.log(response);
                        layer.close(confirm);
                        $('#bootstrapTableEvents').bootstrapTable('refresh');
                        if (response) {
                            if (response.success) {
                                layer.msg(response.msg, {time: 1500, icon: 1});
                            } else {
                                layer.msg(response.msg, {time: 1500, icon: 5});
                            }
                        }
                    })
            });

        },
    },
});

$(function () {
    //初始化表格
    tableInit();
});
